from flask import Blueprint, jsonify, request

bp = Blueprint('main', __name__)

# 模拟数据
structure_data = {
    "hole": {
        "title": "四壁小孔",
        "content": "晾房四壁布满菱形小孔（孔径8-10厘米），呈品字形排列。这些小孔能引导空气形成穿堂风，是葡萄干燥的核心动力，每天可带走葡萄5%左右的水分。"
    },
    "frame": {
        "title": "木架系统",
        "content": "内部木架分3-4层，用本地杨木或柳木制成。每层间距1.2-1.5米，上层通风最佳（5-7天干燥），下层湿度稍高（适合晚熟葡萄），避免阳光直射导致葡萄变质。"
    },
    "roof": {
        "title": "屋顶通风口",
        "content": "尖顶设计便于排水，顶部预留通风口，与四壁小孔形成垂直气流循环，加速热空气排出，保持晾房内干燥凉爽的环境。"
    }
}

process_data = [
    {
        "id": 1,
        "title": "采摘",
        "subtitle": "精选八分熟葡萄",
        "description": "果农在吐鲁番葡萄园手工采摘颗粒饱满、无破损的葡萄，此时葡萄成熟度约为80%——过熟易掉粒，欠熟则甜度不足。",
        "image": "https://picsum.photos/id/102/800/600"
    },
    {
        "id": 2,
        "title": "挂架",
        "subtitle": "疏松悬挂防霉变",
        "description": "工人将葡萄串均匀挂在晾房的木架横杆上，确保串与串之间留有空隙——重叠会导致通风不足，引发霉变。",
        "image": "https://picsum.photos/id/118/800/600"
    },
    {
        "id": 3,
        "title": "干燥",
        "subtitle": "自然通风的魔法",
        "description": "第1天：葡萄饱满有光泽，水分含量约80%，晾房内的穿堂风开始带走表面水分。",
        "image": "https://picsum.photos/id/103/800/600"
    },
    {
        "id": 4,
        "title": "分拣",
        "subtitle": "筛选优质葡萄干",
        "description": "干燥完成后，工人手工分拣葡萄干，去除未干透、破损或有杂质的果实，确保成品品质。",
        "image": "https://picsum.photos/id/127/800/600"
    },
    {
        "id": 5,
        "title": "成品",
        "subtitle": "多元品种与风味",
        "description": "分拣后的葡萄干经过简单清洗（或直接包装），成为我们餐桌上的美味零食。吐鲁番葡萄干以品种丰富、甜度高闻名。",
        "image": "https://picsum.photos/id/139/800/600"
    }
]

drying_data = [
    { "day": 1, "image": "https://picsum.photos/id/103/800/600", "description": "第1天：葡萄饱满有光泽，水分含量约80%，晾房内的穿堂风开始带走表面水分。" },
    { "day": 3, "image": "https://picsum.photos/id/104/800/600", "description": "第3天：葡萄开始变软，表皮出现细微褶皱，水分含量降至65%左右。" },
    { "day": 7, "image": "https://picsum.photos/id/105/800/600", "description": "第7天：果实明显皱缩，体积缩小约1/3，水分含量约40%，糖分开始浓缩。" },
    { "day": 10, "image": "https://picsum.photos/id/117/800/600", "description": "第10天：葡萄干基本成型，表皮干燥有韧性，水分含量约20%。" },
    { "day": 15, "image": "https://picsum.photos/id/137/800/600", "description": "第15天：完全干燥，水分含量≤15%，果肉呈深紫色，甜度达到峰值。" }
]

varieties_data = {
    "white": {
        "name": "无核白",
        "description": "最常见的品种，果肉晶莹剔透，甜度高达24%-26%，适合直接食用或烘焙。"
    },
    "red": {
        "name": "红香妃",
        "description": "果肉呈浅红色，带有淡淡玫瑰香，颗粒较大（长约2厘米），富含花青素。"
    },
    "black": {
        "name": "黑加仑",
        "description": "紫黑色果皮，果肉酸甜适中，富含维生素C和抗氧化物质，适合泡水或做果酱。"
    }
}

@bp.route('/api/structure/<section>')
def get_structure_info(section):
    """获取晾房结构信息"""
    if section in structure_data:
        return jsonify(structure_data[section])
    return jsonify({"error": "Section not found"}), 404

@bp.route('/api/process')
def get_process_steps():
    """获取晾晒步骤"""
    return jsonify(process_data)

@bp.route('/api/drying')
def get_drying_data():
    """获取干燥过程数据"""
    return jsonify(drying_data)

@bp.route('/api/varieties')
def get_varieties():
    """获取葡萄干品种信息"""
    return jsonify(varieties_data)

@bp.route('/api/game/result', methods=['POST'])
def get_game_result():
    """获取游戏结果"""
    data = request.get_json()
    wind_level = data.get('wind_level', 3)
    day_level = data.get('day_level', 10)
    
    # 游戏逻辑：通风3-5级且天数10-15天为最佳
    if wind_level >= 3 and wind_level <= 5 and day_level >= 10 and day_level <= 15:
        result = {
            "status": "success",
            "title": "优质葡萄干！",
            "description": f"恭喜！你用{wind_level}级通风和{day_level}天，晾晒出了含水量15%的优质葡萄干",
            "image": "https://picsum.photos/id/137/800/600"
        }
    elif wind_level < 3:
        result = {
            "status": "fail",
            "title": "晾晒失败",
            "description": f"通风不足（仅{wind_level}级），葡萄霉变了！晾房需要足够的穿堂风才能防止发霉",
            "image": "https://picsum.photos/id/138/800/600"
        }
    elif day_level < 10:
        result = {
            "status": "fail",
            "title": "晾晒不足",
            "description": f"{day_level}天时间太短，葡萄还没干透（水分含量>30%），容易变质",
            "image": "https://picsum.photos/id/105/800/600"
        }
    else:
        result = {
            "status": "fail",
            "title": "过度晾晒",
            "description": f"{day_level}天时间太长，葡萄干变得干硬，失去了柔软口感",
            "image": "https://picsum.photos/id/139/800/600"
        }
    
    return jsonify(result)